package com.mall.malladminbackend.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.malladminbackend.entity.Category;
import com.mall.malladminbackend.entity.GridCategory;
import com.mall.malladminbackend.mapper.CategoryMapper;
import com.mall.malladminbackend.model.PageBean;
import com.mall.malladminbackend.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 作者：郭立仁
 * 描述：永无BUG
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public R selectAll(Integer page,Integer pageSize){
        Page<GridCategory> pages = PageHelper.startPage(page, pageSize);
        categoryMapper.selectAll();
        return R.buildSuccess(0,"success", PageBean.buildPageBean(pages));
    }

    public R selectCate(Integer cateId,Integer page,Integer pageSize){
        Page<GridCategory> pages = PageHelper.startPage(page, pageSize);
        categoryMapper.selectCate(cateId);
        return R.buildSuccess(0,"success",PageBean.buildPageBean(pages));
    }

    @Transactional
    public R deleteCateAndGrid(Integer id){
        int i = categoryMapper.deleteGrid(id);
        int i1 = categoryMapper.deleteCate(id);
        return (i+i1)>0? R.buildSuccess(0,"success") : R.buildSuccess(100,"失败");
    }

    @Transactional
    public R updateGrid(GridCategory gridCategory){
        gridCategory.setUpdateTime(new Date());
        int i = categoryMapper.updateGrid(gridCategory);
        return i>0? R.buildFailure(0,"success") : R.buildSuccess(100,"失败");
    }

    @Transactional
    public R insertGrid(GridCategory gridCategory){
        int i = categoryMapper.insert(gridCategory);
        return i>0? R.buildFailure(0,"success") : R.buildSuccess(100,"失败");
    }

    @Transactional
    public R delCate(Integer id){
        int i = categoryMapper.deleteCateById(id);
        return i>0? R.buildFailure(0,"success") : R.buildSuccess(100,"失败");
    }

    @Transactional
    public R updateCate(Category category){
        category.setUpdateTime(new Date());
        int i = categoryMapper.updateCate(category);
        return i>0? R.buildFailure(0,"success") : R.buildSuccess(100,"失败");
    }

    @Transactional
    public R insertCate(Category category){
        Date date = new Date();
        category.setCreateTime(date);
        category.setUpdateTime(date);
        int i = categoryMapper.insertCate(category);
        return i>0? R.buildFailure(0,"success") : R.buildSuccess(100,"失败");
    }

    public R allCategory(){
        return R.buildSuccess(categoryMapper.allCategory());
    }
}
